package com.m3958.firstgwt.client;

import com.google.gwt.core.client.JsArrayString;
import com.google.inject.Inject;
import com.m3958.firstgwt.client.ui.ActiveIndicator;



public class ScriptCssLoader {
	
	@Inject
	private ActiveIndicator indicator;
	
	private void showIndicator(String msg){
		indicator.show(msg);
	}
	
	private void showIndicator(String msg,int seconds){
		indicator.show(msg,seconds);
	}
	
	private void hideIndicator(){
		indicator.hide();
	}
	
	public void scriptLoadCallback(IwantLoadCssScripts scriptReceiver,String name,boolean success){
		if(success){
			scriptReceiver.onScriptLoadSuccess(name);
		}else{
			scriptReceiver.onScriptLoadFailure(name);
		}
	}
	
	 public void loadScriptViaCallback(ScriptCssLoader sl,String scriptUrl,String scriptsName,String indicatorStr,IwantLoadCssScripts scriptReceiver){
		 JsArrayString scripts = JsArrayString.createArray().cast();
		 scripts.push(scriptUrl);
		 loadScriptsViaCallback(sl, scripts, scriptsName, indicatorStr, scriptReceiver);
	 }
	
	 public final native void loadScriptsViaCallback(ScriptCssLoader sl,JsArrayString scriptUrls,String scriptsName,String indicatorStr,IwantLoadCssScripts scriptReceiver)/*-{
	 	sl.@com.m3958.firstgwt.client.ScriptCssLoader::showIndicator(Ljava/lang/String;)(indicatorStr);
	    $wnd.Y.Get.script(scriptUrls,
		  {
		    onSuccess: function(o) {
				sl.@com.m3958.firstgwt.client.ScriptCssLoader::scriptLoadCallback(Lcom/m3958/firstgwt/client/IwantLoadCssScripts;Ljava/lang/String;Z)(scriptReceiver,scriptsName,true);
				sl.@com.m3958.firstgwt.client.ScriptCssLoader::hideIndicator()();
		    },
		    onFailure: function(o) {
		    	sl.@com.m3958.firstgwt.client.ScriptCssLoader::scriptLoadCallback(Lcom/m3958/firstgwt/client/IwantLoadCssScripts;Ljava/lang/String;Z)(scriptReceiver,scriptsName,false);
		    	sl.@com.m3958.firstgwt.client.ScriptCssLoader::showIndicator(Ljava/lang/String;I)(indicatorStr + "加载失败！",6);
		    },
		    onTimeout: function(o) {
		    	sl.@com.m3958.firstgwt.client.ScriptCssLoader::scriptLoadCallback(Lcom/m3958/firstgwt/client/IwantLoadCssScripts;Ljava/lang/String;Z)(scriptReceiver,scriptsName,false);
		    	sl.@com.m3958.firstgwt.client.ScriptCssLoader::hideIndicator()();
		      	sl.@com.m3958.firstgwt.client.ScriptCssLoader::showIndicator(Ljava/lang/String;I)(indicatorStr + "加载超时！",6);
		    },
		    data: "foo",
		    timeout: 10000, // 10 second timeout
		    context: $wnd//, // make the YUI instance
		    // win: otherframe // target another window/frame
		    //autopurge: true // allow the utility to choose when to
		                    // remove the nodes
		    //purgetheshold: 1 // purge previous transaction before
		                     // next transaction
		  });
	}-*/;
	 
	 public void loadCssViaCallback(ScriptCssLoader sl,String cssUrl,String cssName,String indicatorStr,IwantLoadCssScripts scriptReceiver){
		 JsArrayString csses = JsArrayString.createArray().cast();
		 csses.push(cssUrl);
		 loadCssesViaCallback(sl, csses, cssName, indicatorStr, scriptReceiver);
	 }
	 
	 public final native void loadCssesViaCallback(ScriptCssLoader sl,JsArrayString cssUrls,String cssesName,String indicatorStr,IwantLoadCssScripts cssReceiver)/*-{
	 	sl.@com.m3958.firstgwt.client.ScriptCssLoader::showIndicator(Ljava/lang/String;)(indicatorStr);
	    $wnd.Y.Get.css(cssUrls,
		  {
		    onSuccess: function(o) {
				sl.@com.m3958.firstgwt.client.ScriptCssLoader::scriptLoadCallback(Lcom/m3958/firstgwt/client/IwantLoadCssScripts;Ljava/lang/String;Z)(cssReceiver,cssesName,true);
				sl.@com.m3958.firstgwt.client.ScriptCssLoader::hideIndicator()();
		    },
		    onFailure: function(o) {
		    	sl.@com.m3958.firstgwt.client.ScriptCssLoader::scriptLoadCallback(Lcom/m3958/firstgwt/client/IwantLoadCssScripts;Ljava/lang/String;Z)(cssReceiver,cssesName,false);
		    	sl.@com.m3958.firstgwt.client.ScriptCssLoader::hideIndicator()();
		      	$wnd.Y.log("transaction failed");
		    },
		    onTimeout: function(o) {
		    	sl.@com.m3958.firstgwt.client.ScriptCssLoader::scriptLoadCallback(Lcom/m3958/firstgwt/client/IwantLoadCssScripts;Ljava/lang/String;Z)(cssReceiver,cssesName,false);
		    	sl.@com.m3958.firstgwt.client.ScriptCssLoader::hideIndicator()();
		      	$wnd.Y.log("transaction timed out");
		    },
		    data: "foo",
		    timeout: 10000 // 10 second timeout
//		    insertBefore:''
		  });
	}-*/;
}
